﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using Morus;

namespace KAKA
{
    public class FormsAuthenticationWrapper : IFormsAuthentication
    {
        private const string KEY_AUTHENTICATED = "X-KAKA-AUTHENTICATED";
        private HttpContextBase Context { get; set; }

        public FormsAuthenticationWrapper()
            : this(new HttpContextWrapper(HttpContext.Current))
        {
        }

        public FormsAuthenticationWrapper(HttpContextBase httpContext)
        {
            Context = httpContext;
        }

        public void SetAuthCookie(string userName, bool createPersistentCookie)
        {
            Context.Session[KEY_AUTHENTICATED] = userName;

            FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
        }

        public void SignOut()
        {
            Context.Session[KEY_AUTHENTICATED] = null;

            FormsAuthentication.SignOut();
        }

        public bool IsAuthenticated
        {
            get
            {
                return Context.User.Identity.IsAuthenticated && (Context.Session[KEY_AUTHENTICATED] as string).IsNotEmpty();
            }
        }
    }
}